import axios from "axios";
import store from "@/store";

function initData() {
	let pageTreeContent1 = store.getters['gw/getRealComponentsList'][0][0] || [];
	let pageTreeContent2 = store.getters['gw/getRealComponentsList'][0][1] || [];
	//第一步拿到所有的id
	let requestData = [];
	let requestDataheader = []
	for(var i = 0; i < pageTreeContent1.length; i++) {
		requestDataheader.push({
			id: pageTreeContent1[i].id,
			componentName: pageTreeContent1[i].componentName,
			state: 1
		});
	}
	requestDataheader.forEach(item => {
		startQueryData(item);
	});
	for(var j = 0; j < pageTreeContent2.length; j++) {
		requestData.push({
			id: pageTreeContent2[j].id,
			componentName: pageTreeContent2[j].componentName,
			state: 1
		});
	}
	requestData.forEach(item => {
		startQueryData(item);
	});
}
//开始根据componentName和id值去请求对应的数据
function startQueryData(component) {
	switch(component.componentName) {
		case 'HomeCarousel':
			queryBanner(component.id, component.componentName);
			break;
		case 'NewZone':
			queryNewZone(component.id, component.componentName);
			break;
		case 'activitiesbanner':
			queryallBanner(component.id, component.componentName);
			break;
		case 'activitiesSlidePic':
			queryslidePic(component.id, component.componentName);
			break;
		case 'activitiesOnePro':
			queryOnePro(component.id, component.componentName);
			break;
		case 'activitiesTwoPro':
			queryTwoPro(component.id, component.componentName);
			break;
		case 'activitiesNinePro':
			queryNinePro(component.id, component.componentName);
			break;
		case 'ActivitieCoupons2':
			queryCoupons2(component.id, component.componentName);
			break;
		case 'ActivitieCoupons':
			queryCoupons(component.id, component.componentName);
			break;
		case 'activitiesThreePic':
			querythreePic(component.id, component.componentName);
			break;
		default:
			break;
	}
}
//轮播图片数据
function queryBanner(id, componentName) {
	let url = window.api.goods_host + window.api.home_ad;
	let options = {
		method: "get",
		url,
		params: {
			enterpriseCode: api.enterpriseCode,
			storeCode: localStorage.getItem('storeCode'),
			relGroup: id
		},
	}
	axios(options).then(data => {
		if(data.data.data) {
			getDataStoreChange(id, componentName, data.data.data)
		}
	})
}
//新人专区
function queryNewZone(id, componentName) {
	let url = window.api.goods_host + '/resourceConfig/mobile/newborn';
	let options = {
		method: "get",
		url,
		params: {
			enterpriseCode: api.enterpriseCode,
			storeCode: localStorage.getItem('storeCode'),
			relGroup: id
		},
	}
	axios(options).then(data => {
		if(data.data.data) {
			getDataStoreChange(id, componentName, data.data.data)
		}
	})
}
//顶部大海报数据
function queryallBanner(id, componentName) {
	let url = window.api.goods_host + '/resourceConfig/mobile/posters';
	let options = {
		method: "get",
		url,
		params: {
			enterpriseCode: api.enterpriseCode,
			storeCode: localStorage.getItem('storeCode'),
			relGroup: id
		},
	}
	axios(options).then(data => {
		if(data.data.data) {
			getDataStoreChange(id, componentName, data.data.data)
		}
	})
}
//可滑动图片
function queryslidePic(id, componentName) {
	let url = window.api.goods_host + '/resourceConfig/mobile/slide_pic';
	let options = {
		method: "get",
		url,
		params: {
			enterpriseCode: api.enterpriseCode,
			storeCode: localStorage.getItem('storeCode'),
			relGroup: id
		},
	}
	axios(options).then(data => {
		if(data.data.data) {
			getDataStoreChange(id, componentName, data.data.data)
		}
	})
}
//三张图片
function querythreePic(id, componentName) {
	let url = window.api.goods_host + '/resourceConfig/mobile/three_pic';
	let options = {
		method: "get",
		url,
		params: {
			enterpriseCode: api.enterpriseCode,
			storeCode: localStorage.getItem('storeCode'),
			relGroup: id
		},
	}
	axios(options).then(data => {
		if(data.data.data) {
			getDataStoreChange(id, componentName, data.data.data)
		}
	})
}
//一排两个商品
function queryTwoPro(id, componentName) {
	let url = window.api.goods_host + '/resourceConfig/mobile/two_pro';
	let options = {
		method: "get",
		url,
		params: {
			enterpriseCode: api.enterpriseCode,
			storeCode: localStorage.getItem('storeCode'),
			relGroup: id,
			usePlatform: "wap",
		},
	}
	axios(options).then(data => {
		if(data.data.data) {
			getDataStoreChange(id, componentName, data.data.data)
		}
	})
}
//独个商品
function queryOnePro(id, componentName) {
	let url = window.api.goods_host + '/resourceConfig/mobile/one_pro';
	let options = {
		method: "get",
		url,
		params: {
			enterpriseCode: api.enterpriseCode,
			storeCode: localStorage.getItem('storeCode'),
			relGroup: id,
			usePlatform: "wap",
		},
	}
	axios(options).then(data => {
		if(data.data.data) {
			getDataStoreChange(id, componentName, data.data.data)
		}
	})
}
//九宫格商品
function queryNinePro(id, componentName) {
	let url = window.api.goods_host + '/resourceConfig/mobile/nine_pro';
	let options = {
		method: "get",
		url,
		params: {
			enterpriseCode: api.enterpriseCode,
			storeCode: localStorage.getItem('storeCode'),
			relGroup: id,
			usePlatform: "wap",
		},
	}
	axios(options).then(data => {
		if(data.data.data) {
			getDataStoreChange(id, componentName, data.data.data)
		}
	})
}
//优惠券信息
function queryCoupons(id, componentName) {
	let url = window.api.goods_host + '/resourceConfig/mobile/act_coup';
	let options = {
		method: "get",
		url,
		params: {
			enterpriseCode: api.enterpriseCode,
			storeCode: localStorage.getItem('storeCode'),
			relGroup: id,
		},
	}
	axios(options).then(data => {
		if(data.data.data) {
			getDataStoreChange(id, componentName, data.data.data)
		}
	})
}

function queryCoupons2(id, componentName) {
	let url = window.api.goods_host + '/resourceConfig/mobile/activities_pic_coupons';
	let options = {
		method: "get",
		url,
		params: {
			enterpriseCode: api.enterpriseCode,
			storeCode: localStorage.getItem('storeCode'),
			relGroup: id,
		},
	}
	axios(options).then(data => {
		if(data.data.data) {
			getDataStoreChange(id, componentName, data.data.data)
		}
	})
}
//动态改变模板数据
function getDataStoreChange(id, componentName, data) {
	let dataStore = store.getters['xxy/getActdataStore']
	for(let key in dataStore) {
		if(key == componentName) {
			dataStore[key] = Object.assign({}, dataStore[key] ? dataStore[key] : {}, {
				[id]: data[0] ? data[0][id] : []
			})
			store.commit('xxy/setActdataStore', Object.assign(
				store.getters['xxy/getActdataStore'], {
					[componentName]: dataStore[key]
				}
			))
		}
	}
	/*store.commit('xxy/setDataStore', {
		arr: data.data.data[0][id],
		componentName,
		id
	})*/
}

export default initData